package cn.edu.neusoft.neufooddemo.model;

import java.io.File;

import cn.edu.neusoft.neufooddemo.bean.UserInfo;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.service.UserService;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * Created by Shinelon on 2017/4/10.
 */

public class UserModel extends RetrofitBaseModel {
    private UserService userService;
    public UserModel() {
        this.userService = retrofit.create(UserService.class);
    }

    public void userLogin(String username, String password, final RetrofitListener listener) {
        Call call = userService.login(username, password);
        bindCallback(call,listener,Constants.USERLOGIN);
    }

    public void register(String username, String userpass, String mobilenum,
                         String address, String comment, final RetrofitListener listener) {
        Call call = userService.register(username, userpass, mobilenum, address, comment);
        bindCallback(call,listener,Constants.USEREGISTER);
    }

    public void getUserById(String user_id, final RetrofitListener<UserInfo> listener) {
        Call call = userService.getUserById(user_id);
        bindCallback(call,listener,Constants.GETUSERBYID);
    }
    public void userCollectFood(String user_id, String food_id, final RetrofitListener listener) {
        Call call = userService.userCollectFood(user_id,food_id);
        bindCallback(call,listener,Constants.USERCOLLECTFOOD);
    }
    public void userCollectShop(String user_id, String shop_id, final RetrofitListener listener) {
        Call call = userService.userCollectShop(user_id,shop_id);
        bindCallback(call,listener,Constants.USERCOLLECTSHOP);
    }


    public void updateUserById(String user_id, String username, String userpass, String mobilenum, String address, final RetrofitListener listener)  {
        Call  call = userService.updateUserById(user_id,username,userpass,mobilenum,address);
        bindCallback(call,listener,Constants.UPDATEUSERBYID);
    }

    public void insertOrder(String user_id, String food_id, String num, String sum, String suggesttime, String address,final RetrofitListener listener) {
        Call call = userService.insertOrder(user_id,food_id,num,sum,suggesttime,address);
        bindCallback(call,listener,Constants.INSERTORDER);
    }
    public void insertOrder2(String user_id, String sum, String suggesttime, String address,String items,final RetrofitListener listener) {
        Call call = userService.insertOrder2(user_id,sum,suggesttime,address,items);
        bindCallback(call,listener,Constants.INSERTORDER2);
    }


    public void getAllUserCollection(String user_id, String flag, final RetrofitListener listener) {
        Call call = userService.getAllUserCollection(user_id,flag);
        bindCallback(call,listener,Constants.GETALLUSERCOLLECTION);
    }

    public void changeUserImg(String user_id, String filename, final RetrofitListener listener)
    {
        File file=new File(filename);
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("img", file.getName(), requestFile);
        Call call = userService.changeUserImg(user_id, body);
        bindCallback(call,listener,Constants.INSERTORDER);
    }
}
